<?php
namespace Admin\Controller;
use Think\Controller;
/**
* GuestController
*/
class GuestController extends CommonController
{
	
	//留言列表
	public function index(){
		$mod = M('guestbook');
        //实例化分页类
        $page=new \Think\Page($mod->where($where)->Count(),10);
        //分页设置
        $page->setConfig('prev','上一页');
        $page->setConfig('next','下一页');
        $page->setConfig('first','首页');
        $page->setConfig('last','末页');
        $page->setConfig('theme','%FIRST% %UP_PAGE% %LINK_PAGE% %DOWN_PAGE% %END% %HEADER%');
        $list  = $mod->limit($page->firstRow,$page->listRows)->order('id DESC')->select();
        $this->assign('page',$page->show());//分页显示
        $this->assign('list', $list);
        $this->display();
	}
	//回复留言
    public function reply(){
        $id        = I('id', 0, 'intval');
        if (IS_POST) {
            //获取留言的内容
	        $content = I('reply', '', 'trim');
	        if (!$id) {
	            alertMes('参数错误');
	        }
	        $datas  = array(
	            'id'        => $id,
	            'reply'     => $content,
                'retime'    => time(),
	        );
            //获取回复邮箱的信息
            $info=M("guestbook")->where("id=$id")->find();
            //获取系统邮箱配置
            $list=M("configs")->where("groupid=4")->select();
            //接收邮件主题
            $title=I('title', '', 'trim');
            $data=array();
            //转换邮箱设置数组
            foreach($list as $key=>$value){
                $data[$value['name']]=$value['value'];
            }
            //执行发送邮件、插入回复操作
            if(false !== M('guestbook')->save($datas)){
               if (flase !== sendEmail($info,$data,$content,$title)) {
                    alertMes('回复成功','/admin/guest/index');
                } else {
                    //邮件发送失败
                    alertMes('回复失败');
                }
            }else{
                alertMes('回复失败');
            }
        }
    	$list          = M("Guestbook")->find($id);
        $list['reply'] = htmlspecialchars($list['reply']); 
        $this->assign('list', $list);
        $this->display();
    }

    //删除留言
    public function del(){
        $id        = I('id', 0, 'intval');
        //执行删除
        if (!$id) {
            alertMes('参数错误');
        }
        //执行删除
        if (M('guestbook')->delete($id)) {
            alertMes('删除成功','/admin/guest/index');
        } else {
            alertMes('删除失败');
        }
    }
    //转发至邮箱弹出层
	public function send(){
        $id        = I('id', 0, 'intval');
        $guest=M("guestbook")->where("id=$id")->find();
        $guest_email=M("guestemail")->select();
        $this->assign('guest_email', $guest_email);
        $this->assign('guest', $guest);
        $this->display();
    }
    //执行发送邮件操作
    public function upsend(){
        $id=$_GET['id'];
        //获取转发邮箱的信息
        $info=M("guestemail")->where("id=$id")->find();
        //获取留言的内容
        $content=$_GET['content'];
        //获取系统邮箱配置
        $list=M("configs")->where("groupid=4")->select();
        //接收邮件主题
        $title=$_GET['title'];
        $data=array();
        //转换邮箱设置数组
        foreach($list as $key=>$value){
            $data[$value['name']]=$value['value'];
        }
        //执行发送邮件   
        if(flase !== sendEmail($info,$data,$content,$title)){
            echo 1;
        }else{
            echo 2;
        }
    }
}